
/**
 @ Name：layui.numberInput 计数器
 @ Author：AAA
 @ License：MIT 
 */
 
layui.define('jquery', function(exports){
  var $ = layui.$
  
  //字符常量
  ,MOD_NAME = 'numberInput', ELEM = '.layui-numbox', NUMBOX = ".layui-input-numbox", MINUS = ".layui-btn-numbox-minus", PLUS = ".layui-btn-numbox-plus", STEP = "data-step", MIN = "data-min", MAX = "data-max"
  
  //外部接口
  ,numberInput = {
    index: layui.numberInput ? (layui.numberInput.index + 10000) : 0
    
    //设置全局项
    ,set: function(options){
      var that = this;
      that.config = $.extend({}, that.config, options);
      return that;
    }
    
    //事件监听
    ,on: function(events, callback){
      return layui.onevent.call(this, MOD_NAME, events, callback);
    }
  }
  
  //操作当前实例
  ,thisIns = function(){
    var that = this
    , options = that.config
    , id = options.id || options.index
    , elem = options.elem || ELEM;
    
    return {
      reload: function(options){
        that.render.call(that, options);
      }
      , getCurrentNumber: function () {
        return parseInt($(elem).find(NUMBOX).val());    
      }
      , setInitNumber: function (init) {
        $(elem).find(NUMBOX).val(init);
        that.checkValue($(elem).get(0));
      }
      ,config: options
    }
  }
  
  //构造器
  ,Class = function(options){
    var that = this;
    that.index = ++numberInput.index;
    that.config = $.extend({}, that.config, numberInput.config, options);
    that.render();
  };
  
  //默认配置
  Class.prototype.config = {
    elem: ELEM,
    min:0,
    max:100,
    step:1
  };
  
  //渲染视图
  Class.prototype.render = function(){
    var that = this
    ,options = that.config;

    $(options.elem).each(function (i,e) {
      that.events(e);
      that.checkValue(e);
    });    
  }

  Class.prototype.events = function(reElem){
    let that = this,
    options = that.config,   
    elem = $(reElem),
    numbox = elem.find(NUMBOX),
    btnMinus = elem.find(MINUS),
    btnPlus = elem.find(PLUS),
    step = parseInt(elem.attr(STEP) || options.step);

    btnPlus.off().on("click", function() {
      let val = parseInt(numbox.val());
      numbox.val(val + step);
      numbox.trigger("change");
    });
    btnMinus.off().on("click", function() {
      let val = parseInt(numbox.val());
      numbox.val(val - step);
      numbox.trigger("change");
    });

    numbox.off().on("change", function() {  
      that.checkValue(reElem); 
    });
  }

  Class.prototype.checkValue = function(reElem){
    let that = this,
    options = that.config,  
    elem = $(reElem),
    numbox = elem.find(NUMBOX),
    btnMinus = elem.find(MINUS),
    btnPlus = elem.find(PLUS),
    min = elem.attr(MIN) || options.min,
    max = elem.attr(MAX)|| options.max,
    val = numbox.val();
    
    if (val == null || val == '' || isNaN(val)) {
      numbox[0].value = min || 0;
      btnMinus[0].disabled = min != null;
    } else {
      let intval = parseInt(val);
      if (max != null && !isNaN(max) && val >= parseInt(max)) {        
        intval = max;
        btnPlus[0].disabled = true;
      } else {
        btnPlus[0].disabled = false;
      }
      if (min != null && !isNaN(min) && val <= parseInt(min)) {
        intval = min;
        btnMinus[0].disabled = true;
      } else {
        btnMinus[0].disabled = false;
      }
      numbox[0].value = intval;
    }
  }
  
  //核心入口
  numberInput.render = function(options){
    var ins = new Class(options);
    return thisIns.call(ins);
  };  
  numberInput.render();
  
  exports(MOD_NAME, numberInput);
});